<template>
    <div class="xiangqing">
        <div class="top">
            <div v-for="(item, index) in tableList" :key="index">
                <!-- 标题 title-->
                <!-- <h2 v-if="item.title1" style="text-align: center;background-color: aqua;height: 40px;line-height: 40px;border-radius: 8px;margin-top: 4px;"></h2> -->
                <p v-if="item.title1" class="subTitle"><span class="line"></span>{{ item.title1 }}</p>
                <h3 v-if="item.title" style="height:50px;line-height: 50px;">{{ item.title }}
                    <van-button style="float: right;margin-top: 16px;" @click="handlegotoGJ('')"
                        v-show="item.title == '施工单位'" size="mini" type="info">添加</van-button>
                </h3>
                <!-- 输入框  isFiled-->
                <div v-else-if="item.isFiled"
                    style="display: flex;justify-content: center;align-items: center;color: black;border-bottom: 1px solid #d2d2d2;">
                    <van-field :required="item.isread" :readonly="item.isreadonly" v-model="JFData[item.key]"
                        :label="item.label" />
                </div>
                <!--下拉框 selectPicker-->
                <div v-else-if="item.isSelect">
                    <div v-if="!item.isTime">
                        <selectPicker :bitian="item.isread" :isTime="item.isTime" :ISrequired="true" :values="'DICNAME'"
                            :DICID="'DICID'"  :columns="item.NYDARR" :labels="item.label"
                            :selectValue="JFData[item.key] | inUseName(item.NYDARR)" @getMessage="setValue"></selectPicker>
                    </div>
                    <div v-else>
                        <selectPicker :bitian="item.isread" :isTime="item.isTime" :ISrequired="true"
                            :labels="item.label" :selectValue="JFData[item.key]" @getMessage="setValue"></selectPicker>
                    </div>
                </div>
                <div v-else-if="item.isDanXuan" style="border-bottom: 1px solid #d2d2d2;">
                    <span style="color: red;">*</span>
                    <span
                        style="width: 40%;font-weight: 550;display: inline-block;padding-left: 4px;line-height: 36px;font-size: 14px;">{{
                            item.label }}</span>
                    <!-- <span style="float: right; padding-top: 4px">
                        <van-radio-group v-model="JFData[item.key]" direction="horizontal" @change="radioChange">
                            <van-radio style="margin-bottom: 5px; font-size: 14px" v-for="(item2, index2) in item.children"
                                :key="index2 + 'childrens'" :name="item2.value">{{ item2.name }}
                            </van-radio>
                        </van-radio-group>
                        <van-button style="float: right;margin-top: -26px;" @click="handlegotoGJ('设计')"
                            v-show="item.label == '是否需要设计单位'" size="mini" type="info">添加</van-button>
                    </span> -->
                </div>
                <div v-if="item.isinput && item.isShows">
                    <!-- <p style="margin-top: 4px; font-size: 14px"><span style="color: #ff0000">*</span>{{ item.label
                    }}</p> -->
                    <van-field v-model="JFData[item.key]" type="textarea" label :border="false" disable-default-padding
                        style="background-color: #e4e4e4; margin-top: 8px; min-height: 140px" />
                </div>
            </div>
            <div v-if="lianjianren.length > 0">
                <h2 style="height:50px;line-height: 50px;">
                    联建人
                </h2>
                <van-collapse v-model="activeNames" v-for="(item1, index) in lianjianren" :key="item1.ROW_GUID">
                    <van-collapse-item :title="item1.XM" :name="index">
                        <div v-for="item in lianjian"
                            style="display: flex;justify-content: center;align-items: center;color: black;border-bottom: 1px solid #d2d2d2;">
                            <van-field :required="item.isread" :readonly="item.isreadonly" v-model="item1[item.key]"
                                :label="item.label" />
                        </div>
                    </van-collapse-item>
                </van-collapse>
            </div>
        </div>
        <footer>
            <van-button style="width:90vw" @click="submit" type="info">下一步</van-button>
        </footer>
        <!-- <footer style="display: flex;justify-content: space-around;width: 100%;">
            <van-button style="width:40vw" type="info">保存</van-button>
            <van-button style="width:40vw"  plain type="info">返回</van-button>
        </footer> -->
    </div>
</template>

<script>
import selectPicker from '@/utils/selectPicker'
import { FileServerProxy } from '@/utils/api'
import { GSA_Search1, GSA_Add1 } from '../../../common/commonApi'
export default {
    filters: {
        inUseName: function (msg, data) {
            let name = ''
            data.forEach(item => {
                if (item.DICID == msg) {
                    name = item.DICNAME
                }
            })
            return name
        },
    },
    data() {
        return {
            isreadsL: true,
            activeNames: ['1'],
            isadd: true,
            rowGuid: {},
            lianjianren: [],
            PKID: "",
            JFData: {

            },
            tableList: [
                {
                    title1:'工程信息'
                },
                // {
                //     title: '工程信息'
                // },
                {
                    label: '户主名称',
                    key: 'HOUSEAPPLICANT',
                    isFiled: true,
                    isread: true,
                    isreadonly: true
                },
                {
                    label: '工程名称',
                    key: 'GCMC',
                    isread: true,
                    isFiled: true,
                    isreadonly: true,
                    isTime: false
                },
                {
                    label: '建设地址',
                    key: 'CONSTRUCITONADDRESS_1',
                    isread: true,
                    isFiled: true,
                    isreadonly: true
                },
                {
                    label: '建筑面积(平方米)',
                    key: 'HOUSEFLOORAREA',
                    isFiled: true,
                    isread: false,
                    isreadonly: true
                },
                {
                    label: '建筑高度(米)',
                    key: 'JZZGD',
                    isFiled: true,
                    isread: false,
                    isreadonly: true
                },
                {
                    label: '附属生活污水处理设施',
                    key: 'WSCLFS',
                    isFiled: false,
                    isSelect: true,
                    isread: false,
                    isreadonly: true,
                    isTime: false,
                    NYDARR: [
                        {
                            DICNAME: '接入户用处理设备',
                            DICID: '183',
                        },
                        {
                            DICNAME: '接入集中处理设备',
                            DICID: '184',
                        },
                        {
                            DICNAME: '接入污水管网',
                            DICID: '253',
                        },
                        {
                            DICNAME: '三格式化粪池',
                            DICID: '254',
                        },
                    ],
                },
                {
                    label: '合同金额(万元)',
                    key: 'ZJK',
                    isFiled: true,
                    isread: false,
                    isreadonly: true
                },
                // {
                //     label: '合同工期(天)',
                //     key: 'HTGQ',
                //     isFiled: true,
                //     isread: true,
                //     isreadonly: true
                // },
                {
                    label: '合同工期开始时间',
                    key: 'XXHT_GQKSSJ',
                    isread: false,
                    isSelect: true,
                    isreadonly: true,
                    isTime: true
                },
                {
                    label: '合同工期结束时间',
                    key: 'XXHT_GQJSSJ',
                    isread: false,
                    isSelect: true,
                    isreadonly: true,
                    isTime: true
                }, {
                    label: '备注',
                    key: 'BZ',
                    isFiled: true,
                    isread: false,
                    isreadonly: false
                },
                {
                    title1:'参建单位(人员)'
                },
                {
                    title: '设计单位(人员)'
                },
                {
                    label: '设计单位(人员)',
                    key: 'SJDW',
                    isFiled: true,
                    isDanXuan: false,
                    isread: false,
                    isreadonly: true
                    // children: [
                    //     {
                    //         name: "是",
                    //         key: "IF_SJDW",
                    //         value: 1,
                    //     },
                    //     {
                    //         name: "否",
                    //         key: "IF_SJDW",
                    //         value: 0,
                    //     },
                    // ],
                },
                {
                    label: '设计单位名称',
                    key: 'SJSXM',
                    isFiled: true,
                    isread: false,
                    isreadonly: true
                },
                {
                    label: '设计负责人',
                    key: 'SJDWFZW',
                    isFiled: true,
                    isDanXuan: false,
                    isread: false,
                    isreadonly: true
                },
                {
                    title: '施工单位(人员)'
                },
                {
                    label: '施工单位(人员)',
                    key: 'XM',
                    isFiled: true,
                    isread: false,
                    isreadonly: true
                },
                {
                    label: '施工单位名称',
                    key: 'XM',
                    isFiled: true,
                    isread: false,
                    isreadonly: true
                },
                {
                    label: '施工负责人',
                    key: 'LXR',
                    isFiled: true,
                    isread: false,
                    isreadonly: true
                },
                {
                    title: '监理单位(人员)'
                },
                // {
                //     label: '监理单位(人员)',
                //     key: 'JLDW_SGFZR',
                //     isFiled: true,
                //     isread: false,
                //     isreadonly: false
                // },
                {
                    label: '监理单位名称',
                    key: 'JLDWMC',
                    isFiled: true,
                    isread: false,
                    isreadonly: false
                },
                {
                    label: '监理负责人',
                    key: 'JLDW_SGFZR',
                    isFiled: true,
                    isread: false,
                    isreadonly: false
                },
                {
                    title1:'建筑工程项目明细'
                },
                // {
                //     title: '建筑工程项目明细'
                // },
                {
                    label: '户主姓名',
                    key: 'HOUSEAPPLICANT',
                    isread: true,
                    isFiled: true,
                    isreadonly: true
                },
                {
                    label: '建筑面积(平方米)',
                    key: 'HOUSEFLOORAREA',
                    isread: false,
                    isFiled: true,
                    isreadonly: true
                },
                {
                    label: '地上建筑面积(平方米)',
                    key: 'DSJZMJ',
                    isFiled: true,
                    isread: false,
                    isreadonly: true
                },
                {
                    label: '地下建筑面积(平方米)',
                    key: 'DXJZMJ',
                    isFiled: true,
                    isread: false,
                    isreadonly: true
                },
                {
                    label: '建筑层数',
                    key: 'BUILDSTOREY',
                    isread: false,
                    NYDARR: [
                    ],
                    isSelect: true,
                    isreadonly: true
                },
                {
                    label: '地上建筑层数',
                    key: 'DSJZCS',
                    isread: false,
                    NYDARR: [
                    ],
                    isSelect: true,
                    isreadonly: true
                },
                {
                    label: '地下建筑层数',
                    key: 'DXZJCS',
                    isread: false,
                    NYDARR: [
                    ],
                    isSelect: true,
                    isreadonly: true
                },
            ],
            lianjian: [
                {
                    label: '建筑单位面积(平方米)',
                    key: 'LANDTYPE',
                    isread: false,
                    isFiled: true,
                    isreadonly: true
                },
                {
                    label: '地上建筑面积(平方米)',
                    key: 'DSJZMJ',
                    isFiled: true,
                    isread: false,
                    isreadonly: true
                },
                {
                    label: '地下建筑面积(平方米)',
                    key: 'DXJZMJ',
                    isFiled: true,
                    isread: false,
                    isreadonly: true
                },
                {
                    label: '建筑层数',
                    key: 'BUILDSTOREY',
                    isread: false,
                    NYDARR: [
                    ],
                    isSelect: true,
                    isreadonly: true
                },
                {
                    label: '地上建筑层数',
                    key: 'DSJZCS',
                    isread: false,
                    NYDARR: [
                    ],
                    isSelect: true,
                    isreadonly: true
                },
                {
                    label: '地上建筑层数',
                    key: 'DXZJCS',
                    isread: false,
                    NYDARR: [
                    ],
                    isSelect: true,
                    isreadonly: true
                },
            ]
        }
    },
    props: {
        GJData: {
            type: Object
        }
    },
    created() {
        this.rowGuid = JSON.parse(window.sessionStorage.getItem('fileData'))
        let emitData = [
            'db35edee-88e0-4fae-b14a-868ed4beb1d9', //建房类型
            'e9c98787-4966-4365-80b0-c1cc678c6f85', //市所属
            '77fdf2b0-0eb5-4243-a0a4-6185299ccd34', //县所属
            'e3bdca71-0c96-48d9-b41e-684595a29c80', //工匠选择（工匠、企业、土师傅）
            'a892e0f4-5fd6-42b4-b6c2-6e1fc5ebb5dc', //建筑层数
            'd2ec96d4-b5bc-404a-a9f1-34db915c694b',//地类
        ]
        this._panduan()
        this.getEnum(emitData)
        this.getData()
        console.log(this.rowGuid, 'data')
        try {
            for (let i in this.JFData) {
                this.JFData[i] = this.rowGuid[i]
            }
        } catch (e) { }
    },
    methods: {
        //获取联建人信息
        async getlianjianrenData() {
            let data = [{ "field": "TO_ROW_GUID", "value": this.rowGuid.ROW_GUID }]
            const res = await GSA_Search1('56ed772f-ace1-4ebf-ba2a-04f07020ab72', data)
            console.log(res, 'dfnienfieni')
            this.lianjianren = res.Data
            let newArr = []
            this.lianjianren.forEach(item=>{
                newArr.push(item.XM)
            })
            //newArr.length>0?"、"+newArr.join('、'):''
            this.JFData.GCMC = newArr.length==0?this.JFData.HZXM +'农村住房建筑工程':this.JFData.HZXM +"、"+newArr.join('、')+'农村住房建筑工程'
            console.log(this.JFData.GCMC,'nfuefhui')
        },
        handlegotoGJ(data) {
            //先保存数据
            this.update()
            if (!data) {
                this.$router.push(
                    {
                        path: '/craftsmanServices',
                        query: {
                            id: '施工备案'
                        },
                    }
                )
            } else if (this.isreadsL) {
                this.$router.push(
                    {
                        path: '/appraisalCompany',
                        query: {
                            id: '施工备案'
                        },
                    }
                )
            } else if (!this.isreadsL) {
                this.$toast.fail('不可添加')
            }
        },
        setValue(name, value, labels) {
            if (labels == '项目所在城市') {
                //需要请求对象的区县接口
                this.JFData.XMSZCS = value
                this.getEnum(['77fdf2b0-0eb5-4243-a0a4-6185299ccd34'], [{ field: "pid", value: value, operator: "=" }])
            } else if (labels == '项目所在区县') {
                this.JFData.XMSZQX = value
            }
            if (value == '合同开工日期') {
                this.JFData.HTKGRQ = name
            } else if (value == '合同竣工日期') {
                this.JFData.HTJGRQ = name
            } else if (value == '计划开工日期') {
                this.JFData.JHKGRQ = name
            } else if (value == '计划竣工日期') {
                this.JFData.JHJGRQ = name
            }
        },
        radioChange(value, name) {
            if (value == '0') {
                this.isreadsL = false
            } else {
                this.isreadsL = true
            }
        },
        //获取数据
        async getData() {
         debugger
            let data = {
                accessCode: '688bd3883572440a859dfd8ba9194e7b',
                cityCode: window.localStorage.getItem('citycodeChange') ? window.localStorage.getItem('citycodeChange') : '441300000000',
                params: {
                    dataguid: window.localStorage.getItem('newData')?"f0ed818a-6897-4a9a-96b8-e721afd0249e":"3bc9768e-4a52-48b3-aabe-90c62410bfb0",
                    searchObject: JSON.stringify([{ "field": "ROW_GUID", "value": this.rowGuid.ROW_GUID }]),
                    token: window.localStorage.getItem('token'),
                },
            }
            const res = await FileServerProxy(data)
            const result = JSON.parse(res.data.data).Data[0]
            this.JFData = result
            console.log(this.JFData, 'jkjkjjj')
            //需要用当前行数据填充
            JSON.parse(window.sessionStorage.getItem('fileData')).SNAME = this.JFData.SNAME
            JSON.parse(window.sessionStorage.getItem('fileData')).XNAME = this.JFData.XNAME
            JSON.parse(window.sessionStorage.getItem('fileData')).TOWNNAME = this.JFData.TOWNNAME
            JSON.parse(window.sessionStorage.getItem('fileData')).CUNNAME = this.JFData.CUNNAME
            let DQHSJ = JSON.parse(window.sessionStorage.getItem('fileData'))
            this.JFData.HZXM = DQHSJ.HOUSEAPPLICANT
            this.JFData.HZXM = DQHSJ.HOUSEAPPLICANT
            console.log(JSON.parse(window.sessionStorage.getItem('GJdata')), 'JSON.parse(this.$route.query.id)')
            if (window.sessionStorage.getItem('GJdata')) {
                this.JFData.SGDW_QYMC = JSON.parse(window.sessionStorage.getItem('GJdata')).artisanName
                this.JFData.SGDW_XYDM = JSON.parse(window.sessionStorage.getItem('GJdata')).workerType
                this.JFData.GJPHONE = JSON.parse(window.sessionStorage.getItem('GJdata')).phone
            }
            if (window.sessionStorage.getItem('JZGSData')) {
                this.JFData.SJDW_QYMC = JSON.parse(window.sessionStorage.getItem('JZGSData')).qymc
                this.JFData.SJDW_XYDM = JSON.parse(window.sessionStorage.getItem('JZGSData')).zzjgdm
                //根据统一社会代码查询施工负责人
            }
            this.getlianjianrenData()
            // this.JFData = JSON.parse(res.data.data).Data[0]
        },
        //更新数据
        async update() {
            if (this.isadd) {
                this.JFData.eca_RowState = "added"
                this.JFData.TO_ROW_GUID = this.rowGuid.ROW_GUID
            } else {
                this.JFData.PKID = this.PKID
            }
            if(window.sessionStorage.getItem('newData')){
                GSA_Add1('b77b6a58-3741-4d3b-af90-f6fc88331adb', this.isadd, this.JFData)
            } else {
                GSA_Add1('4929e1f4-3884-4f8f-8d5d-966053e87437', this.isadd, this.JFData)
            }
            GSA_Add1('4929e1f4-3884-4f8f-8d5d-966053e87437', this.isadd, this.JFData)
            this.$emit('handleactive1',3)
        },
        //获取枚举
        getEnum(emitData = [], data = []) {
            emitData.forEach(async (item) => {
                let params = {
                    accessCode: '688bd3883572440a859dfd8ba9194e7b',
                    cityCode: window.localStorage.getItem('citycodeChange') ? window.localStorage.getItem('citycodeChange') : '441300000000',
                    params: {
                        dataguid: item,
                        searchObject: JSON.stringify(data),
                        token: window.localStorage.getItem('token'),
                    },
                }
                const res = await FileServerProxy(params)
                let newData = JSON.parse(res.data.data).Data
                switch (item) {
                    case 'db35edee-88e0-4fae-b14a-868ed4beb1d9':
                        //建房类型
                        this.tableList.forEach((item) => {
                            if (item.label == '建房类型') {
                                item.NYDARR = newData
                            }
                        })
                        break
                    case 'e9c98787-4966-4365-80b0-c1cc678c6f85':
                        //市所属
                        this.tableList.forEach((item) => {
                            if (item.label == '项目所在城市') {
                                newData.forEach(items => {
                                    item.NYDARR.push({
                                        DICNAME: items.CITYNAME,
                                        DICID: items.CITYCODE,
                                    })
                                })
                                console.log(item, 'NY')
                            }
                        })
                        break
                    case '77fdf2b0-0eb5-4243-a0a4-6185299ccd34':
                        this.tableList.forEach((item) => {
                            if (item.label == '项目所在区县') {
                                newData.forEach(items => {
                                    item.NYDARR.push({
                                        DICNAME: items.CITYNAME,
                                        DICID: items.CITYCODE,
                                    })
                                })
                            }
                        })
                        break
                    case 'e3bdca71-0c96-48d9-b41e-684595a29c80':
                        // this.tableList.forEach((item) => {
                        //     if (item.label == '地类') {
                        //         item.NYDARR = newData
                        //     }
                        // })
                        break
                    case 'a892e0f4-5fd6-42b4-b6c2-6e1fc5ebb5dc':
                        this.tableList.forEach((item) => {
                            if (item.label == '建筑层数') {
                                item.NYDARR = newData
                            }
                        })
                        break
                    case 'd2ec96d4-b5bc-404a-a9f1-34db915c694b':
                        this.tableList.forEach((item) => {
                            if (item.label == '地类') {
                                item.NYDARR = newData
                            }
                        })
                        break
                }
            })
        },
        //判断是新增还是修改
        async _panduan() {
            let data = [{ "field": "ROW_GUID", "value": this.rowGuid.ROW_GUID }]
            const res = await GSA_Search1('4929e1f4-3884-4f8f-8d5d-966053e87437', data)
            //当存在返回值时调用修改  无返回值时调用新增
            if (res.Data.length > 0) {
                this.isadd = false
                this.PKID = res.Data[0].PKID
            } else {
                this.isadd = true
            }
        },
        getDiffDay(date_1, date_2) {
            // 计算两个日期之间的差值
            let totalDays, diffDate
            let myDate_1 = Date.parse(date_1)
            let myDate_2 = Date.parse(date_2)
            // 将两个日期都转换为毫秒格式，然后做差
            diffDate = Math.abs(myDate_1 - myDate_2) // 取相差毫秒数的绝对值
            totalDays = Math.floor(diffDate / (1000 * 3600 * 24)) // 向下取整
            // console.log(totalDays)
            return totalDays    // 相差的天数
        },
        timestampToTime(times) {
            let time = times[1]
            let mdy = times[0]
            mdy = mdy.split('/')
            let month = parseInt(mdy[0]);
            let day = parseInt(mdy[1]);
            let year = parseInt(mdy[2])
            return year + '-' + month + '-' + day
        },
        async submit() {
            //先保存
            this.update()
            // 对比当前选择的工匠是否和之前一样、
            // 先判断是否为工匠还是企业
            // 如果不一致需要重新跳转签合同页面
            // let rowData = JSON.parse(window.sessionStorage.getItem('fileData'))
            // let GJdata = JSON.parse(window.sessionStorage.getItem('GJdata'))
            // if (rowData.OPTYPE == 59) {
            //     if (rowData.GSMC != this.JFData.SJDW_QYMC) {
            //         //需要重新跳转签合同
            //         this.$router.push({
            //             path: '/ConstructionContract',
            //             query: {
            //                 data: window.sessionStorage.getItem('fileData'),
            //                 fileData: 1
            //             },
            //         })
            //     }
            // } else {
            //     //自动发送
            //     setTimeout(() => {
            //         this.$router.push({
            //             path: '/TreeSelect',
            //             query: {
            //                 data: window.sessionStorage.getItem('fileData'),
            //                 SJXK: 1,
            //             },
            //         })
            //     }, 500)
            // }
            // if (rowData.OPTYPE == 60) {
            //     if (rowData.GJSFZH != GJdata.idCard) {
            //         //需要重新跳转签合同
            //         this.$router.push({
            //             path: '/ConstructionContract',
            //             query: { data: window.sessionStorage.getItem('fileData'), fileData: 1 },
            //         })
            //     }
            // } else {
            //     //自动发送
            //     setTimeout(() => {
            //         this.$router.push({
            //             path: '/TreeSelect',
            //             query: {
            //                 data: window.sessionStorage.getItem('fileData'),
            //                 SJXK: 1,
            //             },
            //         })
            //     }, 500)
            // }
        }
    },
    components: {
        selectPicker
    }
}
</script>


<style lang="scss" scoped>
.xiangqing {
    height: 100vh;
    position: relative;
    .subTitle {
    width: 100%;
    height: 50px;
    line-height: 50px;
    font-size: 16px;
    position: relative;
    display: flex;
    align-items: center;
    //   border-bottom: 1px solid #9b9b9b;
    padding-left: 10px;

    .line {
      width: 6px;
      height: 26px;
      background: #3f8ffc;
      display: inline-block;
      margin-right: 10px;
    }
  }
    .top {
        height: 80vh;
        overflow-x: auto;

        h3 {
            line-height: 36px;
        }
    }

    footer {
        position: fixed;
        bottom: 10px;
        right: 50%;
        transform: translateX(50%);
    }
}
</style>

<style>
.xiangqing .van-field__label {
    color: black;
    width: 8em;
    font-weight: 550;
}

.xiangqing .van-field__control {
    text-align: right;
}

.xiangqing .van-radio--horizontal {
    margin-right: 50px;
}

.xiangqing .van-cell {
    padding: 7px 8px;
}

.xiangqing .van-cell--required::before {
    left: 2px;
}
</style>
